/*
 * NacteniShowPanel.java
 *
 * Created on 5.12.2007, 19:35:34
 *
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.possoft.client.gui.panel.nastavenispodnipanel.panel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import layout.TableLayout;
import cz.possoft.client.gui.panel.nastavenispodnipanel.components.ExceptionPanel;
import cz.possoft.client.utils.SaveLoadUtils;
import cz.possoft.messages.PropertiesManager;
import cz.possoft.messages.Text;

/**
 *
 * @author mara
 */
public class NacteniShowPanel extends JPanel {

    // list se seznamem drive ulozenych her
    private static JList listOfGames;

    // pole se seznamem her, kterer jsou zobrazeny v listu listOfGames
    private Vector vectorOfGames = new Vector();

    // scrollPane nad listem
    private static JScrollPane scrollPane;

    // tlacitko ktere zpusobi nacteni hry
    private JButton loadButton;

    // spodni labely s hlaskama
    private static ExceptionPanel connectExceptionText;
    
    // nazev hry, ktera byla poslana oponentovi k navrhu
    private static String gameName;

    double[][] size = {{32, 60, 20, 29, 80, 32}, {32, 22, 10, 0, 10, 22, 178, 10, 22, 0, 44}};

    public NacteniShowPanel() {
        setLayout(new TableLayout(size));
        gameName = "";
        initContents();
    }

    public void initContents() {        

        JLabel lab = new JLabel(PropertiesManager.getInstance().getMessageForKey(Text.SEZNAM_ULOZENYCH_SOUBORU));
        lab.setHorizontalAlignment(JLabel.LEFT);
        add(lab,"1,1,4,1");         
        add(getLoadButton(), "1,8,2,8");        
        getListOfGames();
        add(scrollPane, "1,4,4,6");
        //add(new JButton("cc"), "1,9,3,9");        
        add(getConnectExceptionText(), "1,10,4,10");
    }

    public JList getListOfGames() {
        if (listOfGames == null) {
            listOfGames = new JList();
            scrollPane = new JScrollPane(listOfGames);
            scrollPane.setAutoscrolls(false);
            listOfGames.addListSelectionListener(new MyListSelectionListener());
            refreshList();
        }
        return listOfGames;
    }

    public JButton getLoadButton() {
        if (loadButton == null) {
            loadButton = new JButton(PropertiesManager.getInstance().getMessageForKey(Text.LOAD_GAME_BUTTON));
            loadButton.addActionListener(new LoadListener());
        }
        return loadButton;
    }

    public static ExceptionPanel getConnectExceptionText() {
        if (connectExceptionText == null) {
            connectExceptionText = new ExceptionPanel("");
        }
        return connectExceptionText;
    }

    public static String getGameName() {
        return gameName;
    }

    public static void setGameName(String gameNam) {
        gameName = gameNam;
    }        

    // refresne list s jiz ulozenyma hrama z adresare games
    public void refreshList() {
        vectorOfGames.clear();
        String[] p = SaveLoadUtils.getSavedGames();
        for (int i = 0 ; i<p.length; i++) {
            String s = p[i];
            if (s.indexOf(".svg") == s.length() - 4) {
                vectorOfGames.add(transformFileName(s));
            }
        }
        getListOfGames().setListData(vectorOfGames);
        getLoadButton().setEnabled(false);
    }

    // transformuje jmeno souboru pro prijemne zobrazeni v listu
    private String transformFileName(String fileName) {
        String withoutSvg = fileName.substring(0, fileName.length() - 4);
        return "<HTML><BODY><FONT color=red>" + withoutSvg + "</FONT></BODY></HTML>";
    }

// listener zabezpecuje disablovani a enablovani tlacitka na delete souperum.
    private class MyListSelectionListener implements ListSelectionListener {

        public void valueChanged(ListSelectionEvent lse) {
            getLoadButton().setEnabled(true);
        }
    }

    private class LoadListener implements ActionListener {

        // nastava pri stisku tlacitka 'nacist'
        public void actionPerformed(ActionEvent ae) {
            String selected = (String)getListOfGames().getSelectedValue();
            String opponentName = SaveLoadUtils.getUserNameFromListValue(selected);
            // vyparsovane jmeno ze hry se musi rovnat aktualnimu jmenu oponenta. Jinak 
            // vypiseme chybu.
            if (!opponentName.equals(NastaveniShowPanel.getConnectThread().getOpponentName())) {
                getConnectExceptionText().setText(PropertiesManager.getInstance().getMessageForKey(Text.NO_SAME_OPPONENT));
                return;
            }
            getConnectExceptionText().setText(PropertiesManager.getInstance().getMessageForKey(Text.WAIT_FOR_OPPONENT_FOR_LOAD));            
            setGameName(selected);
            NastaveniShowPanel.getConnectThread().sendToServer("LOAD_GAME_VYZVA: ");
//            gameName = selected;
//            Game loadedGame = SaveLoadUtils.loadGame(selected);
//            ChessPanel.setGame(loadedGame);            
        }
    }
}
